import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import {h} from "vue";
import {Tag} from "ant-design-vue";
import {defHttp} from "@/utils/http/axios";
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '序号',
    align:"center",
    dataIndex: 'no',
    width:40,
  },
   {
    title: '下单日期',
    align:"center",
    dataIndex: 'orderDate',
     width:70,
     auth: 'dw:dw_pro_plan:looks'
   },
  {
    title: '排单日期',
    align:"center",
    dataIndex: 'queuedDate',
    width:70,
  },
  {
    title: '订单号',
    align: "center",
    dataIndex: 'orderNo_dictText',
    customRender: function (text) {
      // 判断orderNo_dictText是否存在且有值
      const hasOrderNoDictText = text.record["orderNo_dictText"] !== undefined && text.record["orderNo_dictText"] !== null && text.record["orderNo_dictText"] !== '';
      // 根据是否有值选择不同的显示内容
      const displayText = hasOrderNoDictText
        ? text.record["orderNo_dictText"] + (text.record["childNum"] == null ? '' : "-" + text.record["childNum"]): text.record["bhOrderNo"];

      return h(Tag, {
        color: (text.record["status"] == '3' ? '#417123' :(text.record["status"] == '2' ? '#175bb5' :(text.record["status"] == '1' ? '#b5171c' : '#d78647'))),
      }, displayText)
    },
    width: 120,
  },

  {
    title: '型号',
    align:"center",
    dataIndex: 'machineModel_dictText',
    width:100,

  },

  {
    title: '恒温',
    align:"center",
    dataIndex: 'isHeat_dictText',
    width:60,
    auth: 'dw:dw_pro_plan:looks'
  },
  {
    title: '销售经理',
    align:"center",
    dataIndex: 'salesManager_dictText',
    width: 60,
  },
  {
    title: '客户名称',
    align:"center",
    dataIndex: 'orderId_dictText',
    width: 100,
    auth: 'dw:dw_pro_plan:look',
  },

  {
    title: '喷头',
    align:"center",
    dataIndex: 'printHead_dictText',
    // customRender: ({ record }) => {
    //   return record.printHead_dictText+"["+record.printHeadNum_dictText+"喷头]";
    // },

    customRender: function (text) {//
      return h(Tag, {
        color:  (text.record["printHeadNum_dictText"] == '单' ? '#417123' : '#872222') ,
      }, text.record["printHead_dictText"]+"["+text.record["printHeadNum_dictText"]+"喷头]"  )
    },
    auth: 'dw:dw_pro_plan:looks'
  },

   {
    title: '产线',
    align:"center",
    dataIndex: 'proLine_dictText',
     width:60,
     auth: 'dw:dw_pro_plan:looks'
   },
  {
    title: '下料',
    align:"center",
    dataIndex: 'blanking_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },
  {
    title: '配货',
    align:"center",
    dataIndex: 'distribution_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },

  {
    title: '框架',
    align:"center",
    dataIndex: 'assemble_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },


  {
    title: '运动件',
    align:"center",
    dataIndex: 'assemble1_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },


  {
    title: '电气盒',
    align:"center",
    dataIndex: 'assemble2_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },



  {
    title: '外部线路',
    align:"center",
    dataIndex: 'assemble3_dictColor',
    width: 60,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },
  {
    title: '喷头',
    align:"center",
    dataIndex: 'printHeadStatus_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },
  {
    title: '斜撑平台',
    align:"center",
    dataIndex: 'assemble4_dictColor',
    width: 60,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },
  {
    title: '外壳',
    align:"center",
    dataIndex: 'assemble5_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },


  {
    title: '检验',
    align:"center",
    dataIndex: 'checkout_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },



  {
    title: '打包',
    align:"center",
    dataIndex: 'pack_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },




  {
    title: '发货',
    align:"center",
    dataIndex: 'shipment_dictColor',
    width: 50,
    customRender:({ text }) => {
      return h('div', {
        style: {"background": text, "width":"18px","height":"18px","border-radius":"50%","margin":"0 auto"}
      })
    }
  },


];
//查询数据
export const searchFormSchema: FormSchema[] = [

  {
    label: '订单状态',
    field: 'status',
    component: 'JSelectInput',
    defaultValue: '2',
    componentProps: {
      options: [
        { label: '全部', value: '0' },
        { label: '已检验', value: '5' },
        { label: '生产中', value: '2' },
        { label: '已完成', value: '3' },
        { label: '待生产', value: '1' },
      ],
    },
    colProps: { span: 4},
  },

	{
      label: "订单号",
      field: 'orderNo',
      component: 'Input',
      colProps: {span: 5},
 	},

	{
      label: "型号",
      field: 'machineModel',
      component: 'JCategorySelect',
    componentProps: {
      pcode: 'A01',
      multiple: true
    },
      colProps: {span: 6},
 	},

  {
    label: '作业人员',
    field: 'userId',
    component: 'UserSelect',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 10,
    },
    colProps: {span: 6},
  },

  {
    label: "下单日期",
    field: 'orderDate',
    component: 'RangePicker',
    componentProps: {
      valueType: 'Date',
    },
    colProps: {span: 6},
  },

];

//表单数据
export const formSchema: FormSchema[] = [

  {
    label: '下单日期',
    field: 'orderDate',
    component: 'DatePicker',
    componentProps: {
      showTime: false,
      valueFormat: 'YYYY-MM-DD', // 格式化为后端需要的字符串
      defaultValue: new Date(),
      onChange: (value) => {
        if (window['$form'] && window['$form'].setFieldsValue) {
          window['$form'].setFieldsValue({ orderDate: value });
        }
      }
    },
    defaultValue: (() => {
      const date = new Date();
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`; // 直接返回YYYY-MM-DD字符串
    })(),
    colProps: { span: 11 },
    required: true,
  },
  {
    label: '排单日期',
    field: 'queuedDate',
    component: 'DatePicker',
    componentProps: {
      showTime: false,
      valueFormat: 'YYYY-MM-DD', // 格式化为后端需要的字符串
      defaultValue: new Date(),
      onChange: (value) => {
        if (window['$form'] && window['$form'].setFieldsValue) {
          window['$form'].setFieldsValue({ orderDate: value });
        }
      }
    },
    defaultValue: (() => {
      const date = new Date();
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`; // 直接返回YYYY-MM-DD字符串
    })(),
    colProps: { span: 11 },
    required: true,
  },

  {
    label: '订单号',
    field: 'orderNo',
    required: true,
    component: 'JDictSelectTag',
    componentProps: ({ formModel }) => {
      // 判断model.id是否为空
        const hasId = formModel.id && formModel.id !== '' && formModel.id !== null && formModel.id !== undefined;

        // 根据id是否存在返回不同的dictCode配置
        return {
          dictCode: hasId
            ? 'dw_order,order_num,id ,1=1 order by order_num DESC'
            : 'dw_order,order_num,id,status ="1" order by order_num DESC'
        };
    },
    // componentProps: {
    //   dictCode: 'dw_order,order_num,id,status ="1" order by order_num  DESC ',
    // },

    dynamicDisabled: ({ model }) => {
      const isEdit = !!(model.id && model.id !== '' && model.id !== null && model.id !== undefined);
      if (!isEdit) {
        return false; // 新增未提交：不禁用
      } else {
        return !(model.orderNo === '' || model.orderNo === null || model.orderNo === undefined);
      }
    },
    colProps: { span: 11 },
  },

  {
    label: '调单号',
    field: 'oldOrderNo',
    required: true,
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'dw_order,order_num,id,status !="3" order by order_num  DESC ',
    },
    dynamicDisabled: ({ model }) => {
      const isEdit = !!(model.id && model.id !== '' && model.id !== null && model.id !== undefined);
      if (!isEdit) {
        return false; // 新增未提交：不禁用
      } else {
        return !(model.oldOrderNo === '' || model.oldOrderNo === null || model.oldOrderNo === undefined);
      }
    },
    colProps: { span: 11 },
  },

  {
    label: '备货单',
    field: 'bhOrderNo',
    component: 'ApiSelect',
    colProps: { span: 11 },
    required: true,
    componentProps: {
      // 2. 接口请求（保持你现有的 defHttp 调用，确保返回 {result: [{label: '单号', value: '单号'}]}）
      api: () => defHttp.get({ url: '/dw/dwProPlan/getBhOrderNo' }),
      // 3. 关键：自动选中返回结果中的第一条数据（设为默认值）
      // autoSelectFirst: true,
      // 4. 数值转字符串（根据你的单号格式，若为字符串则设为 true，避免类型问题）
      numberToString: true,
      labelField: 'label', // 后端返回的“显示文本”字段
      valueField: 'value', // 后端返回的“实际值”字段
      resultField: 'result',
      // 7. 可选：禁用下拉选择（避免用户手动修改，完全自动）
      // disabled: true,
      style: { pointerEvents: 'none' },
    }
  },

  {
    label: '子编号',
    field: 'childNum',
    component: 'JSelectInput',
    componentProps: {
      options: [
        { label: '1', value: '1' },
        { label: '2', value: '2' },
        { label: '3', value: '3' },
        { label: '4', value: '4' },
        { label: '5', value: '5' },
        { label: '6', value: '6' },
        { label: '7', value: '7' },
        { label: '8', value: '8' },
        { label: '9', value: '9' },
        { label: '10', value: '10' },
        { label: '11', value: '11' },
        { label: '12', value: '12' },
        { label: '13', value: '13' },
        { label: '14', value: '14' },
        { label: '15', value: '15' },
        { label: '16', value: '16' },
        { label: '17', value: '17' },
        { label: '18', value: '18' },
        { label: '19', value: '19' },
        { label: '20', value: '20' },
      ],
    },
    colProps: { span: 11 },
  },
  {
    field: 'no',
    component: 'Input',
    label: '序号',
    colProps: { span: 11 },

  },
  {
    field: 'machineModel',
    component: 'JCategorySelect',
    label: '型号',
    componentProps: {
      pcode: 'A01',
      // multiple: true
    },
    colProps: { span: 11 },
  },
  {
    label: '备货数量',
    field: 'bhOrderSum',
    component: 'InputNumber',
    colProps: { span: 11 },
    defaultValue: 1,
    required: true,
  },


  {
    label: '产线1',
    field: 'proLine',
    component: 'JCheckbox',
    componentProps: {
      dictCode:'dw_process_line',
    },
    colProps: { span: 11 },
  },
  {
    label: '订单类型',
    field: 'orderType',
    component: 'RadioButtonGroup',
    componentProps: {
      options: [
        { label: '订单', value: '1' },
        { label: '备货单', value: '2' },
        { label: '备货转订单', value: '3' },
        { label: '订单调动', value: '4' },

      ]
    },
    defaultValue:'1',
    colProps: { span: 16 },
  },
  {
    label: '核算月份',
    field: 'monTime',
    component: 'MonthPicker',
    required: true,

  },

	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];

// 高级查询数据
// show:({model})=>{
//   return model.process==='40';
// }
export const superQuerySchema = {
  orderDate: {title: '下单日期',order: 0,view: 'datetime', type: 'string',},
  orderNo: {title: '订单号',order: 1,view: 'text', type: 'string',},
  machineModel: {title: '型号',order: 2,view: 'text', type: 'string',},
  proLine: {title: '产线',order: 3,view: 'text', type: 'string',},
  distribution: {title: '配货',order: 4,view: 'text', type: 'string',},
  assemble: {title: '装配',order: 5,view: 'text', type: 'string',},
  checkout: {title: '检验',order: 6,view: 'text', type: 'string',},
  pack: {title: '打包',order: 7,view: 'text', type: 'string',},
  shipment: {title: '发货',order: 8,view: 'text', type: 'string',},
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData: { type?: string } = {}): FormSchema[] {
  // 1. 从 _formData 中获取 type 参数（流程表单调用时需传递此参数）
  return formSchema;
}



